//
//  UpOpenViewController.m
//  jingdongfang
//
//  Created by MTiMac01 on 2017/8/28.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "UpOpenViewController.h"
#import "TPKeyboardAvoidingTableView.h"
#import "IWTextView.h"
#import "DecimalField.h"
#import "UpSizeVC.h"
#import "UpThemeVC.h"
#import "UpLabelVC.h"
#import "IGOSSManager.h"
#import "SVProgressHUD.h"

#import "IGClassesApi.h"
#import "IGThemesApi.h"
#import "IGUpProductionApi.h"
#import "ArtGalleryViewController.h"
#import "UpPrintVC.h"
#import "IGWebViewController.h"
#import "MyArtViewController.h"

#define nameTag             511//作品名
#define contextTag          512//作品详情

#define authorTag           521//作者
#define writingYearTag      523//创作年代

#define museumCollectionTag 532//馆藏

//#define collectKucTag       532
#define openPriceTag        541//单价
#define openKucTag          542//数量

//#define contextTag          562

@interface UpOpenViewController ()<UITableViewDataSource, UITableViewDelegate, UIPickerViewDataSource, UIPickerViewDelegate>
{
    UIImageView *imageView;
    NSString *publicName;   //  作品名
    NSString *publicContext;  //  作品描述
    
    NSString *authorName;//作者
    //    NSArray *classArr;  //  所有类别
    NSInteger classIndex;  //  类别索引
    NSString *writingYear;//创作年代
    
    NSString *sizeStr;//尺寸
    NSString *kuan;//宽
    NSString *gao;//高
    NSString *museumCollection;//馆藏
    NSString *themeStr;//题材
    NSString *themeId;//题材Id
    NSString *labelStr;//标签
    
    NSString *openPrice;//单价
    NSString *openKuc;//数量
    
    UIPickerView *picker;
    
    NSString *nowYear;
    NSString *imageUrl;
}
@property (weak, nonatomic) IBOutlet TPKeyboardAvoidingTableView *tableV;
@property (nonatomic, strong)IGClassesApi *classApi;
@property (nonatomic, strong)IGThemesApi *themesApi;
@property (nonatomic, strong)IGUpProductionApi *api;
@property (nonatomic, strong)NSMutableArray *classArr;
@property (nonatomic, strong)NSMutableArray *themesArr;
@end

@implementation UpOpenViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[IGOSSManager sharedManager] requestOssToken:IGAliYunFolderTypeProduct];

//    self.title = self.isOpen ? @"上传公开作品" : @"上传私密作品";
    NSString *publicWork = NSLocalizedString(@"Upload Public Works", nil);
    NSString *privateWork = NSLocalizedString(@"Upload Private Works", nil);
    self.title = self.isOpen ? publicWork : privateWork;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewChange:) name:UITextViewTextDidChangeNotification object:nil];
    
//    [self ig_setupRightButtonItemWithTitle:@"帮助" image:nil highLightedImage:nil];
    [self ig_setupRightButtonItemWithTitle:NSLocalizedString(@"Help", nil) image:nil highLightedImage:nil];
    [self loadData];
    [self loadTable];

    
}

- (void)loadData
{
    self.classApi = [IGClassesApi new];
    self.themesApi = [IGThemesApi new];
    self.classArr = [@[] mutableCopy];
    self.themesArr = [@[] mutableCopy];
    @KWeakObj(self)
    [self.classApi startWithDecodeClass:[IGClassesResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject)
        {
            IGClassesResp *resp = responseObject;
            IGClassesModel *model = resp.data;
            selfWeak.classArr = [NSMutableArray arrayWithArray:model.productTypes];
            if (self.model.worksTypeContent)
            {
                for (int i = 0; i < _classArr.count; i++)
                {
                    NSString *str = _classArr[i][@"title"];
                    if ([str isEqualToString:self.model.worksTypeContent])
                    {
                        classIndex = i;
                    }
                }
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:1];
                [selfWeak.tableV reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }
        }
    }];
    
    [self.themesApi startWithDecodeClass:[IGThemesResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject)
        {
            IGThemesResp *resp = responseObject;
            IGThemesModel *model = resp.data;
            selfWeak.themesArr = [NSMutableArray arrayWithArray:model.themes];
        }
    }];
    
    
    if (self.model)
    {
        publicName = _model.worksName;
        publicContext = _model.worksContent;
        authorName = _model.worksAthena;
        writingYear = _model.worksYears;
        openPrice = _model.worksCollectPrice;
        openKuc = _model.worksCollectNumLimited;
        NSString *str = @"";
        if (_model.worksLabels.count > 0)
        {
            NSArray *biaoQian = [NSArray arrayWithArray:_model.worksLabels];
            for (int i = 0; i < biaoQian.count; i++)
            {
                if ([str isEqualToString:@""] || !str)
                {
                    str = biaoQian[i];
                }else{
                    str = [str stringByAppendingString:[NSString stringWithFormat:@" %@", biaoQian[i]]];
                }
            }
        }
        labelStr = str;

        NSString *str1 = @"";
        if (_model.worksThemeContent.length > 0 && _model.worksTheme.length > 0)
        {
            NSArray *themeArr = [_model.worksThemeContent componentsSeparatedByString:@","];
            for (int i = 0; i < themeArr.count; i++)
            {
                if ([str1 isEqualToString:@""] || !str1)
                {
                    str1 = themeArr[i];
                }else{
                    str1 = [str1 stringByAppendingString:[NSString stringWithFormat:@" %@", themeArr[i]]];
                }
            }
            themeId = _model.worksTheme;
        }
        themeStr = str1;        
    }
    else
    {
        NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:KLanguage];
        if ([language containsString:@"en"]) {
            publicName = [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"Works of ", nil),[IGUser getNike]];
            publicContext = [NSString stringWithFormat:@"%@%@", NSLocalizedString(@"Works Description of ", nil) ,[IGUser getNike]];
        }else{
            publicName = [NSString stringWithFormat:@"%@%@",[IGUser getNike],NSLocalizedString(@"Works of ", nil)];
            publicContext = [NSString stringWithFormat:@"%@%@", [IGUser getNike],NSLocalizedString(@"Works Description of ", nil)];
        }
        authorName = [IGUser getNike]?:NSLocalizedString(@"Author", nil);
        themeStr = @"";
        labelStr = @"";
    }
    //    writingYear = @"创作年代";
    //    museumCollection = @"馆藏";
    //    openPrice = @"1.00";
    //    openKuc = @"1";
    classIndex = -1;
    sizeStr = @"";
    NSDate *data = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval ss = data.timeIntervalSince1970;
    nowYear = [NSDate timestampToString:[NSString stringWithFormat:@"%@", @(ss)] Format:@"yyyy"];
}
- (void)loadTable
{
    self.tableV.backgroundColor = [UIColor c2Color];
    self.tableV.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENWIDTH*1080/1920)];
    header.backgroundColor = [UIColor c2Color];
    imageView = [[UIImageView alloc] initWithFrame:header.bounds];
    imageView.image = self.image;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [header addSubview:imageView];
    self.tableV.tableHeaderView = header;
    
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 90)];
    UIButton *sure = [UIButton buttonWithType:UIButtonTypeCustom];
    sure.layer.cornerRadius = 20;
    sure.layer.masksToBounds = YES;
    sure.backgroundColor = [UIColor appColor];
    sure.frame = CGRectMake(0, 15, KSCALE(1100), 45);
    sure.titleLabel.font = [UIFont systemFontOfSize:16];
    [sure setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [sure setTitle:@"提 交" forState:UIControlStateNormal];
    [sure setTitle:NSLocalizedString(@"Submit", nil) forState:UIControlStateNormal];
    [sure addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
    sure.centerX = footer.width/2;
    [footer addSubview:sure];
    self.tableV.tableFooterView = footer;
}

- (void)setImage:(UIImage *)image
{
    _image = image;
    imageView.image = image;
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _isOpen ? 4 : 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 2;
    }
    else if (section == 1)
    {
        return 3;
    }
    else if (section == 2)
    {
        return 4;
    }
    else if (section == 3)
    {
        return 2;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.textLabel.textColor = [UIColor c4Color];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:16];
    cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
    cell.detailTextLabel.textColor = [UIColor c5Color];
    CGFloat x = cell.separatorInset.left;
    
    if (indexPath.section==0)
    {
        if (indexPath.row == 0)
        {
            UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(x+5, 0, KSCREENWIDTH-2*x, 50)];
//            field.placeholder = @"作品名(必填)";
            NSString *workName = NSLocalizedString(@"Works' Name", nil);
            NSString *rString = NSLocalizedString(@"Required", nil);
            field.placeholder = [NSString stringWithFormat:@"%@(%@)",workName,rString];
            field.textColor = [UIColor c5Color];
            field.text = publicName;
            field.textAlignment = NSTextAlignmentLeft;
            field.borderStyle = UITextBorderStyleNone;
            field.clearButtonMode = UITextFieldViewModeWhileEditing;
            field.font = cell.textLabel.font;
            field.tag = nameTag;
            [field addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
            [cell.contentView addSubview:field];
        }
        else if (indexPath.row == 1)
        {
            IWTextView *field = [[IWTextView alloc] initWithFrame:CGRectMake(x, 0, KSCREENWIDTH-2*x, 95)];
            field.textColor = [UIColor c5Color];
//            field.placeholder = @"描述一下你的作品(必填)";
            NSString *describeWork = NSLocalizedString(@"Describe Your Works", nil);
            NSString *rString = NSLocalizedString(@"Required", nil);
            field.placeholder = [NSString stringWithFormat:@"%@(%@)",describeWork,rString];
            field.text = publicContext;
            field.textAlignment = NSTextAlignmentLeft;
            field.tag = contextTag;
            field.font = cell.textLabel.font;
            [cell.contentView addSubview:field];
        }
    }
    else if (indexPath.section==1)
    {
        if (indexPath.row == 0)
        {
//            cell.textLabel.text = @"作者(必填)";
            NSString *authorString = NSLocalizedString(@"Author", nil);
            NSString *rString = NSLocalizedString(@"Required", nil);
            cell.textLabel.text = [NSString stringWithFormat:@"%@(%@)",authorString,rString];
            UITextField *textfield = [[UITextField alloc]init];
            textfield.width = 150;
            textfield.height = cell.height - 1;
            textfield.centerY = cell.centerY;
            textfield.borderStyle = UITextBorderStyleNone;
            textfield.keyboardType = UIKeyboardTypeDefault;
            textfield.font = [UIFont systemFontOfSize:16];
            textfield.textColor = [UIColor c5Color];
            textfield.textAlignment = NSTextAlignmentRight;
            [textfield addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
            textfield.tag = authorTag;
            textfield.text = authorName;
//            textfield.placeholder = @"请输入作者";
            textfield.placeholder = NSLocalizedString(@"Author Name", nil);
            cell.accessoryView = textfield;
        }
        else if (indexPath.row == 1)
        {
//            cell.textLabel.text = @"类别(必选)";
//            cell.detailTextLabel.text = @"请选择类别";
            NSString *categoryStr = NSLocalizedString(@"Type", nil);
            NSString *rString = NSLocalizedString(@"Required", nil);
            cell.textLabel.text = [NSString stringWithFormat:@"%@(%@)",categoryStr,rString];
            cell.detailTextLabel.text = NSLocalizedString(@"Choose type", nil);
            if (classIndex>=0)
            {
                cell.detailTextLabel.text = [_classArr[classIndex][@"title"] length] > 0 ? _classArr[classIndex][@"title"] : @"";
            }
            cell.detailTextLabel.textColor = [UIColor c5Color];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        else if (indexPath.row == 2)
        {
//            cell.textLabel.text = @"创作年代(必填)";
            NSString *ageString = NSLocalizedString(@"Creation Age", nil);
            NSString *rString = NSLocalizedString(@"Required", nil);
            cell.textLabel.text = [NSString stringWithFormat:@"%@(%@)",ageString,rString];
            UITextField *textfield = [[UITextField alloc]init];
            textfield.width = 150;
            textfield.height = cell.height - 1;
            textfield.centerY = cell.centerY;
            textfield.borderStyle = UITextBorderStyleNone;
            textfield.textAlignment = NSTextAlignmentRight;
            textfield.keyboardType = UIKeyboardTypeNumberPad;
            textfield.font = [UIFont systemFontOfSize:15];
            textfield.textColor = [UIColor c5Color];
//            textfield.placeholder = @"请选择创作年代";
            textfield.placeholder = NSLocalizedString(@"Creation Age", nil);
            [textfield addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
            textfield.tag = writingYearTag;
            textfield.text = writingYear;
            cell.accessoryView = textfield;
        }
    }
    else if (indexPath.section==2)
    {
        if (indexPath.row==0)
        {
//            cell.textLabel.text = @"尺寸";
            cell.textLabel.text = NSLocalizedString(@"Width cm/Height cm", nil);
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.detailTextLabel.text = sizeStr;
        }
        else if (indexPath.row==1)
        {
//            cell.textLabel.text = @"馆藏";
            cell.textLabel.text = NSLocalizedString(@"Collection", nil);
            UITextField *textfield = [[UITextField alloc]init];
            textfield.width = 150;
            textfield.height = cell.height - 1;
            textfield.centerY = cell.centerY;
            textfield.borderStyle = UITextBorderStyleNone;
            textfield.keyboardType = UIKeyboardTypeDefault;
            textfield.textAlignment = NSTextAlignmentRight;
            textfield.font = [UIFont systemFontOfSize:16];
            textfield.textColor = [UIColor c5Color];
            [textfield addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
//            textfield.placeholder = @"请输入馆藏";
            textfield.placeholder = NSLocalizedString(@"Collection", nil);
            textfield.tag = museumCollectionTag;
            textfield.text = museumCollection;
            cell.accessoryView = textfield;
        }
        if (indexPath.row==2)
        {
//            cell.textLabel.text = @"题材(必填)";
            cell.textLabel.text = [NSString stringWithFormat:@"%@(%@)",NSLocalizedString(@"Subject", nil),NSLocalizedString(@"Required", nil)];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.detailTextLabel.text = themeStr;
            UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(x, 0, KSCREENWIDTH-x-x, .5)];
            line.backgroundColor = [UIColor c6Color];
            [cell.contentView addSubview:line];
        }
        if (indexPath.row==3)
        {
//            cell.textLabel.text = @"标签(必填)";
            cell.textLabel.text = [NSString stringWithFormat:@"%@(%@)",NSLocalizedString(@"Mark", nil),NSLocalizedString(@"Required", nil)];
            cell.textLabel.centerY = cell.height / 2;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
            cell.detailTextLabel.text = labelStr;
            cell.detailTextLabel.numberOfLines = 2;
        }
    }
    else if (indexPath.section==3)
    {
        if (indexPath.row == 0)
        {
//            cell.textLabel.text = @"单价";
            cell.textLabel.text = NSLocalizedString(@"Unit Price", nil);

            UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectZero];
            label2.font = cell.textLabel.font;
//            label2.text = @"元";
            label2.text = NSLocalizedString(@"RMB", nil);
            label2.textColor = [UIColor c5Color];
            [cell.contentView addSubview:label2];
            [label2 sizeToFit];
            label2.right = KSCREENWIDTH - 20;
//            label2.width = 20;
            label2.centerY = cell.centerY;
            
            DecimalField *price = [[DecimalField alloc] initWithFrame:CGRectMake(label2.left - 150, 0, 150, cell.height)];
            price.borderStyle = UITextBorderStyleNone;
            price.clearButtonMode = UITextFieldViewModeWhileEditing;
            price.font = cell.textLabel.font;
            price.placeholder = @"0.0";
            price.textColor = [UIColor c5Color];
            price.text = openPrice;
            price.tag = openPriceTag;
            price.textAlignment = NSTextAlignmentRight;
            [cell.contentView addSubview:price];
            [price addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
        }
        else if (indexPath.row == 1)
        {
//            cell.textLabel.text = @"数量";
            cell.textLabel.text = NSLocalizedString(@"Quantity", nil);

            UITextField *textfield = [[UITextField alloc]init];
            textfield.textAlignment = NSTextAlignmentRight;
            textfield.width = 150;
            textfield.height = cell.height - 1;
            textfield.centerY = cell.centerY;
            textfield.borderStyle = UITextBorderStyleNone;
            textfield.keyboardType = UIKeyboardTypeNumberPad;
            textfield.font = [UIFont systemFontOfSize:16];
            textfield.textColor = [UIColor c5Color];
//            textfield.placeholder = @"请输入数量";
            textfield.placeholder = NSLocalizedString(@"Add Amount", nil);
            [textfield addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
            textfield.tag = openKucTag;
            textfield.text = openKuc;
            cell.accessoryView = textfield;
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        if (indexPath.row == 1)
        {
            return 100;
        }
        return 50;
    }
    //    else if (indexPath.section == 2)
    //    {
    //        if (indexPath.row == 3)
    //        {
    //
    //        }
    //    }
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 3)
    {
        UIView *gray = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 50)];
        gray.backgroundColor = [UIColor c2Color];
        
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, KSCREENWIDTH - 40, 20)];
        label1.textColor = [UIColor c4Color];
        label1.backgroundColor = [UIColor clearColor];
//        label1.text = @"电子版收藏";
        label1.text = NSLocalizedString(@"E-copy Collection", nil);
        label1.font = [UIFont systemFontOfSize:16];
        label1.textAlignment = NSTextAlignmentLeft;
        [gray addSubview:label1];
        
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(20, label1.bottom, KSCREENWIDTH - 40, 35)];
        label2.textColor = [UIColor c5Color];
        label2.backgroundColor = [UIColor clearColor];
//        label2.text = @"画屏端无限时欣赏, 并无下载权限, 有效保护您的电子版权";
        label2.text = NSLocalizedString(@"Unlimited Screen Display ONLY , NOT for download. Restrict copy right protection.", nil);
        label2.font = [UIFont systemFontOfSize:13];
        label2.numberOfLines=0;
        label2.textAlignment = NSTextAlignmentLeft;
        [gray addSubview:label2];
        
        return gray;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 3)
    {
        return 65;
    }
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return .01f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)
    {
        if (indexPath.row ==1)
        {
            [self showMune];
        }
    }
    else if (indexPath.section == 2)
    {
        @KWeakObj(self)
        if (indexPath.row == 0)
        {
            //尺寸
            UpSizeVC *vc = [[UpSizeVC alloc] initWithNibName:@"UpSizeVC" bundle:nil];
            vc.sureBlock = ^(NSString *sizeSt, NSString *kuanStr, NSString *gaoStr) {
                sizeStr = sizeSt;
                kuan = kuanStr;
                gao = gaoStr;
                [selfWeak.tableV reloadData];
            };
            [self.navigationController pushViewController:vc animated:YES];
        }
        else if (indexPath.row == 2)
        {
            //题材
            UpThemeVC *vc = [[UpThemeVC alloc] init];
            vc.dataSource = self.themesArr;
            vc.sureBlock = ^(NSString *str, NSString *strId) {
                themeStr = str;
                themeId = strId;
                [selfWeak.tableV reloadData];
            };
            [self.navigationController pushViewController:vc animated:YES];
        }
        else if (indexPath.row == 3)
        {
            //标签
            UpLabelVC *vc = [[UpLabelVC alloc] initWithNibName:@"UpLabelVC" bundle:nil];
            vc.sureBlock = ^(NSString *str) {
                labelStr = str;
                [selfWeak.tableV reloadData];
            };
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

#pragma mark - UIPickerViewDataSource, UIPickerViewDelegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _classArr.count?:0;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *text = [[UILabel alloc] initWithFrame:CGRectZero];
    text.font = [UIFont systemFontOfSize:15];
    text.text = [_classArr[row][@"title"] length] > 0 ? _classArr[row][@"title"] : @"";
    [text sizeToFit];
    return text;
}

#pragma mark - showMune

- (void)showMune
{
    UIControl *ctrl = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT)];
    ctrl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    ctrl.tag = 868;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, KSCREENHEIGHT-250, KSCREENWIDTH, 250)];
    view.backgroundColor = [UIColor whiteColor];
    CGFloat height = [self createSubview:view];
    view.height = height;
    view.y = KSCREENHEIGHT-height;
    [ctrl addSubview:view];
    ctrl.alpha = 0;
    
    [ctrl addTarget:self action:@selector(ctrlAction:) forControlEvents:UIControlEventTouchUpInside];
    [[UIApplication sharedApplication].keyWindow addSubview:ctrl];
    
    [UIView animateWithDuration:.2 animations:^{
        ctrl.alpha = 1;
    }];
}

- (CGFloat)createSubview:(UIView *)view
{
    UIView *nav = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 44)];
    nav.backgroundColor = [UIColor whiteColor];
    [view addSubview:nav];
    
    UIButton *left = [UIButton buttonWithType:UIButtonTypeCustom];
    left.frame = CGRectMake(10, 0, 50, 44);
    left.titleLabel.font = [UIFont systemFontOfSize:16];
//    [left setTitle:@"取消" forState:UIControlStateNormal];
    [left setTitle:NSLocalizedString(@"Cancel", nil) forState:UIControlStateNormal];
    [left setTitleColor:[UIColor appColor] forState:UIControlStateNormal];
    [left addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [nav addSubview:left];
    
    UIButton *right = [UIButton buttonWithType:UIButtonTypeCustom];
    right.frame = CGRectMake(KSCREENWIDTH-70, 0, 60, 44);
    right.titleLabel.font = [UIFont systemFontOfSize:16];
//    [right setTitle:@"确定" forState:UIControlStateNormal];
    [right setTitle:NSLocalizedString(@"Confirm", nil) forState:UIControlStateNormal];
    [right setTitleColor:[UIColor appColor] forState:UIControlStateNormal];
    [right addTarget:self action:@selector(doneAction:) forControlEvents:UIControlEventTouchUpInside];
    [nav addSubview:right];
    
    if (!picker)
    {
        picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, KSCREENWIDTH, view.height-44)];
        picker.backgroundColor = [UIColor whiteColor];
        picker.delegate = self;
        picker.dataSource = self;
        picker.showsSelectionIndicator = YES;
    }
    [view addSubview:picker];
    
    if (classIndex>=0)
    {
        [picker selectRow:classIndex inComponent:0 animated:YES];
    }
    
    [picker reloadAllComponents];
    return picker.height+44;
}

- (void)doneAction:(UIButton *)btn
{
    if (_classArr.count < 1)
    {
        [self hiddenCtrl];
        return;
    }
    NSInteger row =[picker selectedRowInComponent:0];
    classIndex = row;
    
    [self.tableV reloadData];
    [self hiddenCtrl];
}

- (void)ctrlAction:(UIControl *)ctrl
{
    [self hiddenCtrl];
}

- (void)cancelAction:(UIButton *)btn
{
    [self hiddenCtrl];
}

- (void)hiddenCtrl
{
    UIControl *ctrl = [[UIApplication sharedApplication].keyWindow viewWithTag:868];
    
    [UIView animateWithDuration:.2 animations:^{
        ctrl.alpha = 0;
    } completion:^(BOOL finished) {
        [ctrl removeFromSuperview];
    }];
}


- (void)textChange:(UITextField *)textField
{
    if (textField.tag == nameTag)
    {
        publicName = textField.text;
    }
    else if (textField.tag == authorTag)
    {
        authorName = textField.text;
    }
    else if (textField.tag == writingYearTag)
    {
        if ([textField.text integerValue] > [nowYear integerValue])
        {
            textField.text = writingYear;
            //            [textField resignFirstResponder];
//            [self presentMessageTips:@"创作年代时间不对"];
            [self presentMessageTips:NSLocalizedString(@"Wrong Creation Time", nil)];
            //            return;
        }
        writingYear = textField.text;
    }
    else if (textField.tag == museumCollectionTag)
    {
        museumCollection = textField.text;
    }
    else if (textField.tag == openPriceTag)
    {
        openPrice = textField.text;
    }
    else if (textField.tag == openKucTag)
    {
        openKuc = textField.text;
    }
}

- (void)textViewChange:(NSNotification *)not
{
    UITextView *textView = not.object;
    if ([textView isMemberOfClass:[IWTextView class]])
    {
        if (textView.tag == contextTag)
        {
            publicContext = textView.text;
        }
    }
}

#pragma mark — 上传作品

- (void)sureAction:(UIButton *)btn
{
    [self.view endEditing:YES];
    if (publicName.length==0)
    {
//        [self presentMessageTips:@"请输入作品名称"];
        [self presentMessageTips:NSLocalizedString(@"Please Input Works Name", nil)];
        return;
    }
    if ([Tool convertToByte:publicName] >= 20)
    {
//        [self presentMessageTips:@"作品名称过长"];
        [self presentMessageTips:NSLocalizedString(@"Works Name Overlong", nil)];

        return;
    }
    if (publicContext.length==0)
    {
//        [self presentMessageTips:@"请输入作品描述"];
        [self presentMessageTips:NSLocalizedString(@"Please Input Works Description", nil)];

        return;
    }
    if ([Tool convertToByte:publicContext] >= 200)
    {
//        [self presentMessageTips:@"作品描述过长"];
        [self presentMessageTips:NSLocalizedString(@"Works Description Too Long", nil)];

        return;
    }
    //公开
    if (_isOpen)
    {
        if (authorName.length == 0)
        {
//            [self presentMessageTips:@"请输入作者"];
            [self presentMessageTips:NSLocalizedString(@"Please Input Author Name", nil)];

            return;
        }
        if (classIndex < 0)
        {
//            [self presentMessageTips:@"请选择作品类别"];
            [self presentMessageTips:NSLocalizedString(@"Please Select Works Category", nil)];

            return;
        }
        if (writingYear.length == 0)
        {
//            [self presentMessageTips:@"请填写创作年代"];
            [self presentMessageTips:NSLocalizedString(@"Please Input Creation Time", nil)];

            return;
        }
        if (museumCollection.length!=0 && [Tool convertToByte:museumCollection] >= 20)
        {
//            [self presentMessageTips:@"馆藏数量过长"];
            [self presentMessageTips:NSLocalizedString(@"Excessive Collection Quantity", nil)];

            return;
        }
        if (themeStr.length==0)
        {
//            [self presentMessageTips:@"至少选择一种题材"];
            [self presentMessageTips:NSLocalizedString(@"Select at Least One Theme", nil)];

            return;
        }
        if (labelStr.length==0)
        {
//            [self presentMessageTips:@"请输入标签"];
            [self presentMessageTips:NSLocalizedString(@"Please Input Mark", nil)];

            return;
        }
        if (openKuc != nil && ![Tool isIntNumber:openKuc])
        {
//            [self presentMessageTips:@"数量填写错误"];
            [self presentMessageTips:NSLocalizedString(@"Wrong Quantity", nil)];

            return;
        }
        
        if ([openKuc integerValue] > 99999999)
        {
//            [self presentMessageTips:@"数量不能超过99999999"];
            [self presentMessageTips:NSLocalizedString(@"Quantity <99999999", nil)];

            return;
        }
        
        if ([openPrice integerValue] > 10000)
        {
//            [self presentMessageTips:@"单价不能超过10000"];
            [self presentMessageTips:NSLocalizedString(@"Unit Price < RMB 10,000", nil)];

            return;
        }
    }
#pragma mark === 上传
    if (self.model)
    {
        [self uploadData];
    }
    else
    {
        [self uploadMainImageData];
    }
}

- (void)uploadMainImageData
{
    NSArray *uploadArr = @[self.originImage];//拍照上传的直接传image
    @KWeakObj(self)
//    [self showText:@"正在提交..."];
    [self showText:NSLocalizedString(@"Submitting", nil)];
    [[IGOSSManager sharedManager] asyncPutImages:uploadArr withCompleteBlock:^(NSArray *params, NSArray *urls) {
        if (params.count != 0)
        {
            [selfWeak dismissLoading];
            imageUrl = params.firstObject;
            [selfWeak uploadData];
        }
        else
        {
            [selfWeak dismissLoading];
        }
    } progressBlock:nil isProduct:YES];
}

- (void)uploadData
{
    @KWeakObj(self)
    self.api = [IGUpProductionApi new];
    _api.pId = _model.worksId ? : @"";
    _api.athena = authorName;
    _api.title = publicName;
    _api.content = publicContext;
    _api.classType = classIndex >= 0 ? _classArr[classIndex][@"id"] : @"";
    _api.plates = _isH ? @"1" : @"2";
    _api.kuan = kuan?:@"";
    _api.gao = gao?:@"";
    _api.collection = museumCollection?:@"";
    _api.theme = themeId?:@"";
    _api.labels = labelStr?:@"";
    _api.collPrice = openPrice?:@"0";
    _api.collNums = openKuc?:@"";
    _api.image = imageUrl;
    _api.years = writingYear;
    _api.isOpen = self.isOpen;
    _api.cropUrl = _cropUrl;
    _api.width = [NSString stringWithFormat:@"%@", @(_originImage.size.width)];
    _api.height = [NSString stringWithFormat:@"%@", @(_originImage.size.height)];
    [self.api startWithDecodeClass:[IGUpProductionResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject)
        {
             [UIViewController ig_delectViewController:[ArtGalleryViewController class]];
//            [selfWeak presentMessageTips:@"提交成功!"];
//            [selfWeak presentMessageTips:NSLocalizedString(@"Report successfully!", nil)];
            [selfWeak presentMessageTips:NSLocalizedString(@"works upload successfully", nil)];
            ArtGalleryViewController *artVC = [[ArtGalleryViewController alloc] init];
            if (_isOpen)
            {
                artVC.index = 1;
            }
            else
            {
                artVC.index =0;
            }
            [UIViewController ig_pushViewController:artVC animated:YES];
//            [UIViewController ig_delectViewController:[UpOpenViewController class]];
//            [UIViewController ig_delectViewController:[UpPrintVC class]];
            
            NSArray *currentVC = self.navigationController.viewControllers;
            NSMutableArray *allVC = [NSMutableArray arrayWithArray:currentVC];
            for (UIViewController *vc in currentVC)
            {
                if ([vc isKindOfClass:[UpOpenViewController class]])
                {
                    [allVC removeObject:vc];
                }
                if ([vc isKindOfClass:[UpPrintVC class]])
                {
                    [allVC removeObject:vc];
                }
            }
            self.navigationController.viewControllers = allVC;
        }
    }];
}

- (void)ig_rightButtonPressed:(id)sender
{
    IGWebViewController *vc = [[IGWebViewController alloc] initWithType:IGWebViewTypeSix];
    [UIViewController ig_pushViewController:vc animated:YES];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


@end
